if SERVER then
	AddCSLuaFile ("shared.lua")
	SWEP.AutoSwitchTo		= true
	SWEP.AutoSwitchFrom		= false
else
	surface.CreateFont ("inc_CSSelectIcons", {name="csd", size=ScreenScale( 70 ), additive=true})
	surface.CreateFont ("inc_HL2SelectIcons", {name="HL2MP", size=ScreenScale( 70 ), additive=true})
end

SWEP.Author				= "Devenger"
SWEP.Contact			= ""
SWEP.Purpose			= ""
SWEP.Instructions		= ""

SWEP.Spawnable			= true
SWEP.AdminSpawnable		= true

SWEP.PrintName 			= "Incursion Base Weapon"
SWEP.Category			= "Incursion Weaponry"

SWEP.Primary.ClipSize		= -1
SWEP.Primary.DefaultClip	= -1
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "none"

SWEP.Primary.DamageMultiplier	= 0.3

SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"

SWEP.ViewModel				= "models/weapons/v_pistol.mdl"
SWEP.WorldModel				= "models/weapons/w_pistol.mdl"
SWEP.CSMuzzleFlashes		= true

SWEP.HoldType				= "pistol"

function SWEP:Initialize()
	if SERVER then
		self:SetWeaponHoldType (self.HoldType)
	end
end

function SWEP:PrimaryAttack ()
	if not self:CanPrimaryAttack() then
		self:Reload()
		return
	end
	
	self:FireEffects ()	
	self:FireShot ()
	self:TakePrimaryAmmo (1)
	
	self:AddRecoil(math.Rand(-1, -0.5) * self.Primary.Kick, math.Rand(-1, 1) * self.Primary.Kick)
	
	self.Weapon:SetNextPrimaryFire (CurTime() + self.Primary.Delay)
end

function SWEP:CanPrimaryAttack()
 	if self.Weapon:Clip1() <= 0 then
		self.Weapon:SetNextPrimaryFire (CurTime() + 0.1)
 		return false
 	end

 	return true
end

function SWEP:Reload()
	self.Weapon:DefaultReload (ACT_VM_RELOAD)
	// This is to stop the reload glitch on servers without CS:S content.
	if self.ReloadLength then
		self:SetNextPrimaryFire(CurTime() + self.ReloadLength)
		self:SetNextSecondaryFire(CurTime() + self.ReloadLength)
	end
end

function SWEP:Deploy ()
	if not self:CanPrimaryAttack() then
		self:Reload()
	end
end

function SWEP:FireEffects ()
	self.Weapon:EmitSound (self.Primary.Sound)
	self.Weapon:SendWeaponAnim (ACT_VM_PRIMARYATTACK)
	self.Owner:SetAnimation (PLAYER_ATTACK1)
end

function SWEP:FireShot ()
	local cone = self.Primary.Cone or 0.05
	
	local bullet = {} 
 	bullet.Num 		= self.Primary.NumShots or 1
 	bullet.Src 		= self.Owner:GetShootPos()
	bullet.Dir 		= self.Owner:GetLockonAimVector() or self.Owner:GetAimVector()
 	bullet.Spread 	= Vector (cone, cone, 0)
 	bullet.Tracer	= 0
 	bullet.Force	= 6
 	bullet.Damage	= (self.Primary.Damage or 10) * self.Primary.DamageMultiplier
	bullet.Attacker	= self.Entity
	
	self.Owner:FireBullets (bullet)
end

function SWEP:SecondaryAttack ()
	
end

function SWEP:DrawWeaponSelection (x, y, wide, tall, alpha)
	local font = "inc_CSSelectIcons"
	if self.IconFont == "HL2MPTypeDeath" then font = "inc_HL2SelectIcons" end
 	draw.SimpleText (self.IconLetter, font, x + wide/2, y + tall*0.6, Color (255, 210, 0, alpha), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
end

function SWEP:GetViewModelPosition (pos, ang, inv, mul)
	--[[local DefPos = self.ViewModelDefPos
	local DefAng = self.ViewModelDefAng
	
	if DefAng then
		ang = ang * 1
		ang:RotateAroundAxis (ang:Right(), 		DefAng.x)
		ang:RotateAroundAxis (ang:Up(), 		DefAng.y)
		ang:RotateAroundAxis (ang:Forward(), 	DefAng.z)
	end

	if DefPos then
		local Right 	= ang:Right()
		local Up 		= ang:Up()
		local Forward 	= ang:Forward()
	
		pos = pos + DefPos.x * Right
		pos = pos + DefPos.y * Forward
		pos = pos + DefPos.z * Up
	end]]
	
	return pos, ang
end

function SWEP:FirePlasmaBolt (x,y,z)
	local bolt = ents.Create ("inc_plasmabolt")
	local pos = self.Owner:GetShootPos() + self.Owner:GetAimVector() * 10
	pos = pos + (self.Owner:GetForward() * x) + (self.Owner:GetRight() * y) + (self.Owner:GetUp() * z)
	bolt:SetPos (pos)
	local ang = self.Owner:EyeAngles() + Angle ((math.random() - 0.5) * self.Primary.Cone * 90,(math.random() - 0.5) * self.Primary.Cone * 90,(math.random() - 0.5) * self.Primary.Cone * 90)
	bolt:SetAngles (ang)
	bolt:Spawn()
	bolt:SetOwner (self.Owner)
	bolt:SetWeapon (self.Weapon)
	bolt:SetSpeed (self.Primary.BoltSpeed or 250)
	bolt:SetDamage (self.Primary.Damage or 10)
end

SWEP.Recoil, SWEP.LastRecoil = Angle( 0, 0, 0 ), 0
function SWEP:AddRecoil( p, y )
	self.Recoil = self.Recoil + Angle( p, y, 0 )
end
function SWEP:DoRecoil()
	if !( CLIENT || ( SERVER && game.SinglePlayer() ) ) then return end
	
	// We use the difference instead of frametime so it loses recoil after we put it away.
	local delta = CurTime() - self.LastRecoil
	self.LastRecoil = CurTime()
	
	// Subtract a fraction of the remaining recoil so it disappears over time.
	local mult = math.Clamp( delta * 40, 0, 1 )
	self.Recoil.p = math.Approach( self.Recoil.p, 0, mult * math.abs( self.Recoil.p ) )
	self.Recoil.y = math.Approach( self.Recoil.y, 0, mult * math.abs( self.Recoil.y ) )
	
	// We only need to mess with eye angles if there is recoil.
	if ( self.Recoil.p != 0 || self.Recoil.y != 0 ) then
		// Add a fraction of the remaining recoil to the owners eye angles.
		local eyes = self.Owner:EyeAngles()
			eyes = eyes + Angle( self.Recoil.p * mult, self.Recoil.y * mult, 0 )
		self.Owner:SetEyeAngles( eyes )
	end
end
function SWEP:Think()
	self:DoRecoil()
end
